"""
帖子工具包
"""
from application.entity.NoteEntity import NoteOut, NoteAdd, NoteEdit
from application.enumeration.ContentTypeEnum import ContentTypeEnum
from application.enumeration.StatisticsTypeEnum import StatisticsTypeEnum
from application.logic.ImageLogic import batch_add_image
from application.mapper.StatisticsMapper import StatisticsMapper
from application.mapper.ImageMapper import ImageMapper
from application.mapper.VideoMapper import VideoMapper
from application.model.VideoModel import VideoModel
from application.logic.VideoLogic import add_video
from application.model.NoteModel import NoteModel
from typing import Optional, Union


async def note_model_to_note_out(note_model: NoteModel) -> NoteOut:
    """
    帖子模型转帖子输出实体
    :param note_model: 帖子模型
    :return: 输出实体
    """
    # 帖子信息转换为实体
    note_out: NoteOut = NoteOut(**note_model.to_dict())
    # 获取视频URL
    video_model: Optional[VideoModel] = await VideoMapper.get_data_by_field(md5=note_model.video_id)
    note_out.video = video_model.url if video_model else None
    # 获取图片URL列表
    image_model_list: list = [await ImageMapper.get_data_by_field(md5=image_md5) for image_md5 in
                              note_model.image_ids.split(",")]
    note_out.image_list = [image_model.url for image_model in image_model_list if image_model]
    # 获取各统计数
    note_out.like_num = await StatisticsMapper.get_count_by_field(target_id=note_model.id,
                                                                  content_type=ContentTypeEnum.NOTE,
                                                                  name=StatisticsTypeEnum.LIKE)
    note_out.forward_num = await StatisticsMapper.get_count_by_field(target_id=note_model.id,
                                                                     content_type=ContentTypeEnum.NOTE,
                                                                     name=StatisticsTypeEnum.FORWARD)
    note_out.share_num = await StatisticsMapper.get_count_by_field(target_id=note_model.id,
                                                                   content_type=ContentTypeEnum.NOTE,
                                                                   name=StatisticsTypeEnum.SHARE)
    note_out.comment_num = await StatisticsMapper.get_count_by_field(target_id=note_model.id,
                                                                     content_type=ContentTypeEnum.NOTE,
                                                                     name=StatisticsTypeEnum.COMMENT)
    note_out.view_num = await StatisticsMapper.get_count_by_field(target_id=note_model.id,
                                                                  content_type=ContentTypeEnum.NOTE,
                                                                  name=StatisticsTypeEnum.VIEW)
    return note_out


async def static_url_to_id(note_input: Union[NoteAdd, NoteEdit], note_model: NoteModel) -> NoteModel:
    """
    输入实体中的静态URL转为存入数据库的md5-index
    :param note_input: 帖子输入实体
    :param note_model: 帖子模型
    :return: 转换后的帖子模型
    """
    # 图片MD5索引列表转换为字符串，赋值给image_ids
    image_md5_list: list = await batch_add_image(image_list=note_input.image_list) if note_input.image_list else []
    note_model.image_ids = ",".join(image_md5_list)
    # 视频URL转换为视频MD5，赋值给video_id
    note_model.video_id = await add_video(video_url=note_input.video) if note_input.video else None
    return note_model
